/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows;
namespace Oceanside
{
    public partial class ControlPanelWindow : PosWindow
    {

        private ControlPanelWindow()
            : base()
        {
            this.InitializeComponent();

            ///Hard code the financial button to green for now.
            this.bFinancial.ChangeButtonColor(Button_164_62.COLORS.FINANCIAL);
            this.bTimeClock.ChangeButtonColor(Button_164_62.COLORS.TIMECLOCK);
            this.Height = PosWindow.MainPosWindow.Height;
            this.Width = PosWindow.MainPosWindow.Width;

            ///If this is quick service, then replace the new table button with a new order
            ///button.
            if (Constants.IsQuickServiceMode)
            {
                for (int i = 0; i < wrapPanelFunctions.Children.Count; i++)
                {
                    UIElement uc = wrapPanelFunctions.Children[i];
                    if (uc is NewTableButton)
                    {
                        ///Remove the old.
                        wrapPanelFunctions.Children.RemoveAt(i);

                        ///Create the new.
                        NewOrderButton nob = new NewOrderButton();
                        nob.MouseDown += ButtonDown;
                        nob.SetBinding(UIElement.VisibilityProperty,new Binding("IsVisible"));
                        nob.Name = "bNewOrder";
                        nob.Margin = new Thickness(4, 0, 0, 6);
                        nob.Tag = "9";
                        Binding dataContext = new Binding()
                        {
                            RelativeSource = RelativeSource.Self,
                            Converter = new IndexToCpBindingConverter(),
                        };
                        nob.DataContext = dataContext;
                        wrapPanelFunctions.Children.Insert(i, nob);
                    }
                }
            }
        }

        private static System.Action prepareForScreenChangeAction;
        public static System.Action PrepareForScreenChangeAction
        {
            get { return prepareForScreenChangeAction; }
            set { prepareForScreenChangeAction = value; }
        }

        private static System.Action prepareForScreenLeaveAction;
        public static System.Action PrepareForScreenLeaveAction
        {
            get { return prepareForScreenLeaveAction; }
            set { prepareForScreenLeaveAction = value; }
        }

        static ControlPanelWindow singleton = null;
        static ControlPanelWindow()
        {
            singleton = new ControlPanelWindow();
        }
        /// <summary>
        /// 
        /// </summary>
        public static WrapPanel PanelFunctions
        {
            get { return singleton.wrapPanelFunctions; }
        }

        /// <summary>
        /// 
        /// </summary>
        public static ListBox OpenTables
        {
            get { return singleton.listBoxOpenTables; }
        }

        /// <summary>
        /// 
        /// </summary>
        public static Control ExitButton
        {
            get { return singleton.cancelButton; }
        }

        public static Control OpenTablesDownButton
        {
            get { return singleton.downButton; }
        }

        public static Control OpenTablesUpButton
        {
            get { return singleton.upButton; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void PrepareForScreenChange()
        {
            PrepareForScreenChangeAction();
        }

        /// <summary>
        /// 
        /// </summary>
        public override void PrepareForScreenLeave()
        {
            PrepareForScreenLeaveAction();
        }

        /// <summary>
        /// 
        /// </summary>
        public static void Show()
        {
            SwitchWindowDelegate_NOTHROW(singleton);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="o"></param>
        public static void AssignDataContext(object o)
        {
            singleton.DataContext = o;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tc"></param>
        public static void AddInstanceToTabControl(ref TabControl tc)
        {
            tc.Items.Add(singleton);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        public static void SetTabIndex(int index)
        {
            singleton.MyTabIndex = index;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                e.Handled = true;
                UserControl uc = sender as UserControl;
                int index = System.Convert.ToInt32(uc.Tag);
                PosWindow.CpListBoundFunctions[index].Clicked();
            }
            catch (System.Exception ex)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_CPBIND_CALL_BOUND_FUNC, ex);
            }
        }

#if _TESTMODE
        public static void DownButton(int funcNumber)
        {
            Button_164_62 bNewOrder = (Button_164_62)PanelFunctions.Children[6];
            switch (funcNumber)
            {
                case 0:
                    bNewOrder.Down();
                    break;
            }
        }

        public static void Exit()
        {
            singleton.cancelButton.Down();
        }
#endif
    }
}
